package com.waoqi.docgen;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yami.shop.api.controller.AddrController;
import com.yami.shop.bean.app.dto.UserAddrDto;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 重置返回参数
 * 重置返回DEMO成功返回数据
 * <p>
 * 重置接口请求参数
 * <p>
 * 生成POST-Man测试数据
 *
 * @author yi.jian
 * @since 2020-03-06
 */
public class ApiDocMain {
    private static final String api_platform = "http://api.waoqi.com/code/codeGen.do";
    private static final String http_servlet_resp = "HttpServletResponse";

    //实体所在的包
    private static final String domain_path = "com.yami.shop.api.controller";
    //接口作者名称
    private static String author = "doc陈";
    //api平台接口存到响应作者
    private static String updateBy = "userName：xiaoyu.chen | trueName：" + author;
    //api平台上生成项目id
    public static String projectId = "165216457799507000359";
    //响应模块id(非必填)，来源于接口平台(在项目下可以创建模块，模块对应的id)
    //private static String moduleId = "161078707241209002064";
    private static String moduleId = "165216570816109000362";
    //postman测试脚本存放路径
    public static final String postman_json = "D:\\xiaoyuworksprace\\resources";


    //生成文档或测试数据入口
    public static void main(String[] args) {

        //生成postman测试数据
        List<Map<String, String>> interfaceList = Lists.newLinkedList();
//        HashMap<String, Object> map = Maps.newHashMap();
//
//       String defineReq = JSONObject.toJSONString(map);
        //defineReq 对应方法的自定义基本类型参数
        //String defineReq = " {\"userId\":\"用户ID\",\"orderNumber\":\"订单号\"}";
        Map<String, String> list = initInterfaceDoc(AddrController.class, "list", null, ResponseEntity.class, null);
//        Map<String, String> getInfo = initInterfaceDoc(UserViewRecordApiController.class, "studyAchieveList", UserViewRecord.class, AcademyUserFinish.class, null);
//        Map<String, String> add = initInterfaceDoc(UserViewRecordApiController.class, "studyRecordList", UserViewRecord.class, null, null);
//
//        Map<String, String> edit = initInterfaceDoc(UserViewRecordApiController.class, "updateStudyRecord", UserViewRecordReqDTO.class, null, null);
//        //Map<String, String> byUserName = initInterfaceDoc(UserViewRecordApiController.class, "byUserName", null, UserInfo.class, defineReq);
//        //Map<String, String> byPhone = initInterfaceDoc(UserViewRecordApiController.class, "byPhone", null, UserInfo.class, defineReq);
//
//        Map<String, String> getDictDataList = initInterfaceDoc(DictDataApiController.class, "getDictDataList", null, DictData.class, defineReq);


        interfaceList.add(list);
//        interfaceList.add(getInfo);
//        interfaceList.add(add);
//        interfaceList.add(edit);
        //interfaceList.add(remove);
        //interfaceList.add(byUserName);
        //interfaceList.add(byPhone);
        //interfaceList.add(getDictDataList);
        //Postman2Entity.newModule("订单查询", interfaceList);


        //保存到接口平台(可单独生成)
        saveToApiPlatform(list);
//        saveToApiPlatform(getInfo);
//        saveToApiPlatform(add);
//        saveToApiPlatform(edit);
        //saveToApiPlatform(byUserName);
        //saveToApiPlatform(byPhone);
        //saveToApiPlatform(getDictDataList);

    }

    //生成接口数据，并保存到接口平台
    public static void saveToApiPlatform(Map<String, String> interfaceMapReq) {
        List<Map<String, String>> interfaceMap = Lists.newArrayList(interfaceMapReq);
        Map<String, Object> request = Maps.newHashMap();
        request.put("requestMap", JSONObject.toJSONString(interfaceMap, SerializerFeature.WriteMapNullValue));
        System.out.println(JSONObject.toJSONString(interfaceMap, SerializerFeature.WriteMapNullValue));
        HttpUtils.doPost(api_platform, request);
    }


    //初始化接口文档
    public static Map<String, String> initInterfaceDoc(Class controllerClass, String methodName, Class reqClass, Class returnClass, String fieldNameKeyDesc) {
        if (StringUtils.isEmpty(methodName)) {
            System.out.println("错误: 暂时不支持当前模式生成接口文档");
            return Maps.newHashMap();
        }
        Method[] methods = controllerClass.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.getName().equalsIgnoreCase(methodName)) {
                continue;
            }
            readFile(controllerClass);
            Map<String, String> fieldNameMap = JSONObject.parseObject(fieldNameKeyDesc, Map.class);
            return buildInterfaceMap(method, controllerClass, reqClass, returnClass, fieldNameMap);
        }
        return Maps.newHashMap();
    }


    // 请求参数mock
    static String buildParamsMock(String param) {
        Map<String, Object> resultMap = Maps.newHashMap();
        param = param.replace("form=", "");
        List<Map<String, Object>> requestMap = JSONObject.parseObject(param, List.class);
        for (Map<String, Object> map : requestMap) {
            String name = String.valueOf(map.get("name"));
            String type = String.valueOf(map.get("type"));
            resultMap.put(name,
                    getValue(type, String.valueOf(map.get("name"))));
        }
        return JSONObject.toJSONString(resultMap, SerializerFeature.WriteMapNullValue);
    }

    // 请求参数mock
    static String buildPostmanParamsMock(String param) {
        Map<String, Object> resultMap = Maps.newHashMap();
        param = param.replace("form=", "");
        List<Map<String, Object>> requestMap = JSONObject.parseObject(param, List.class);
        for (Map<String, Object> map : requestMap) {
            String name = String.valueOf(map.get("name"));
            String type = String.valueOf(map.get("type"));
            String remark = String.valueOf(map.get("remark"));
            resultMap.put(name + "#" + type + "#" + remark,
                    getValue(type, String.valueOf(map.get("name"))));
        }
        return JSONObject.toJSONString(resultMap, SerializerFeature.WriteMapNullValue);
    }

    // 请求参数
    static String buildParams(Parameter[] parameters, Map<String, String> fieldNameMap) {
        List<Map<String, Object>> parametersMap = Lists.newLinkedList();
        for (Parameter parameter : parameters) {
            if ((parameters[0].getType() + "").indexOf(domain_path) != -1) {
                return getDomainForm(parameters[0].getType(), parametersMap, fieldNameMap);
            }
            if ((parameters[0].getType() + "").indexOf(http_servlet_resp) != -1) {
                continue;
            }
            Map<String, Object> requestMap = Maps.newHashMap();
            requestMap.put("name", parameter.getName());
            requestMap.put("def", parameter.getType());
            requestMap.put("remark", parameter.getName());
            if (fieldNameMap != null && fieldNameMap.get(parameter.getName()) != null) {
                requestMap.put("remark", fieldNameMap.get(parameter.getName()));
            }
            requestMap.put("necessary", true);
            requestMap.put("inUrl", false);
            requestMap.put("type", parameter.getType().getSimpleName());
            parametersMap.add(requestMap);
        }
        return "form=" + JSONObject.toJSONString(parametersMap, SerializerFeature.WriteMapNullValue);
    }

    private static String getDomainForm(Class className, List<Map<String, Object>> parametersMap, Map<String, String> fieldNameMap) {
        if (className == null) {
            for (Map.Entry<String, String> stringEntry : fieldNameMap.entrySet()) {
                Map<String, Object> requestMap = Maps.newHashMap();
                requestMap.put("name", stringEntry.getKey());
                requestMap.put("def", "");
                requestMap.put("remark", stringEntry.getValue());
                requestMap.put("necessary", true);
                requestMap.put("inUrl", false);
                requestMap.put("type", "String");
                parametersMap.add(requestMap);
            }
            return "form=" + JSONObject.toJSONString(parametersMap, SerializerFeature.WriteMapNullValue);
        }
        readRequestDOFile(className);
        Field[] fields = className.getDeclaredFields();
        for (Field field : fields) {
            boolean isStatic = Modifier.isStatic(field.getModifiers());
            if (isStatic) {
                continue;
            }
            Map<String, Object> requestMap = Maps.newHashMap();
            requestMap.put("name", field.getName());
            requestMap.put("def", "");
            requestMap.put("remark", methodFieldNameReq.get(field.getName()));
            requestMap.put("necessary", true);
            requestMap.put("inUrl", false);
            requestMap.put("type", field.getType().getSimpleName());
            parametersMap.add(requestMap);
        }
        return "form=" + JSONObject.toJSONString(parametersMap, SerializerFeature.WriteMapNullValue);
    }

    private static String[] postOrGet(Method method) {
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        PutMapping putMapping = method.getAnnotation(PutMapping.class);
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        String[] methodAndUri = new String[2];
        if (requestMapping != null) {
            methodAndUri[0] = requestMapping.method().length != 1 ? "POST" : requestMapping.method()[0].name();
            methodAndUri[1] = Joiner.on(",").join(requestMapping.value());
        }
        if (getMapping != null) {
            methodAndUri[0] = "GET";
            methodAndUri[1] = Joiner.on(",").join(getMapping.value());
        }

        if (postMapping != null) {
            methodAndUri[0] = "POST";
            methodAndUri[1] = Joiner.on(",").join(postMapping.value());
        }
        if (putMapping != null) {
            methodAndUri[0] = "PUT";
            methodAndUri[1] = Joiner.on(",").join(putMapping.value());
        }
        if (deleteMapping != null) {
            methodAndUri[0] = "DELETE";
            methodAndUri[1] = Joiner.on(",").join(deleteMapping.value());
        }
        return methodAndUri;
    }

    private static String getModuleUri(Class controllerClass) {
        try {
            RequestMapping requestMapping = (RequestMapping) controllerClass.getDeclaredAnnotation(RequestMapping.class);
            return Joiner.on(",").join(requestMapping.value());
        } catch (Exception e) {
            return "";
        }
    }


    private static Map<String, String> buildInterfaceMap(Method method, Class controllerClass, Class reqClass, Class returnClass, Map<String, String> fieldNameMap) {
        Map<String, String> interfaceMap = Maps.newHashMap();
        interfaceMap.put("updateBy", updateBy);
        interfaceMap.put("header", "[]");
        interfaceMap.put("projectId", projectId);
        // interfaceMap.put("contentType", contentType);
        interfaceMap.put("moduleld", moduleId);

        String interfaceName = methodClass.get(method.getName() + "_comment");
        interfaceMap.put("interfaceName", method.getName());
        if (StringUtils.isNotEmpty(interfaceName)) {
            interfaceMap.put("interfaceName", interfaceName);
        }
        interfaceMap.put("remark", methodClass.get(controllerClass.getSimpleName()));
        interfaceName = interfaceMap.get("remark") + "-" + interfaceMap.get("interfaceName") + "-" + author;
        interfaceMap.put("interfaceName", interfaceName);

        String[] postOrGet = postOrGet(method);
        interfaceMap.put("url", getModuleUri(controllerClass) + postOrGet[1]);

        interfaceMap.put("method", postOrGet[0]);
        if (reqClass != null || MapUtils.isNotEmpty(fieldNameMap)) {
            interfaceMap.put("param", getDomainForm(reqClass, Lists.newLinkedList(), fieldNameMap));//{"name":"userId","def":"","remark":"用户id","necessary":"true","inUrl":"false","type":"string"}
        } else {
            interfaceMap.put("param", buildParams(method.getParameters(), fieldNameMap));//{"name":"userId","def":"","remark":"用户id","necessary":"true","inUrl":"false","type":"string"}
        }
        interfaceMap.put("paramRemark", buildParamsMock(interfaceMap.get("param")));//POSTman 请求参数
        interfaceMap.put("postmanRemark", buildPostmanParamsMock(interfaceMap.get("param")));//POSTman 请求参数

        if (returnClass != null) {
            interfaceMap.put("resopnseParam", buildResopnseParam(returnClass.getName()));//{"name":"success","remark":"","necessary":"true","type":"string"}
        } else {
            interfaceMap.put("resopnseParam", buildResopnseParam(methodClass.get(method.getName() + "_result_class_package")));//{"name":"success","remark":"","necessary":"true","type":"string"}
        }
        interfaceMap.put("trueExam", JSONObject.toJSONString(trueExamMock, SerializerFeature.WriteMapNullValue));//json
//        interfaceMap.put("falseExam", JSONObject.toJSONString(AjaxResult.error(CodeMsg.SERVER_ERROR, "mock异常")));//json
        return interfaceMap;
    }

    static Map<String, Object> trueExamMock = Maps.newHashMap();

    private static String buildResopnseParam(String className) {
        if (StringUtils.isEmpty(className)) {
            return "[]";
        }
        List<Map<String, Object>> fieldMapList = Lists.newLinkedList();
        try {
            Map<String, Object> retcode = Maps.newHashMap();
            retcode.put("name", "code");
            retcode.put("remark", "返回码");
            retcode.put("necessary", "");
            retcode.put("type", "int");
            trueExamMock.put("code", 200);
            Map<String, Object> msg = Maps.newHashMap();
            msg.put("name", "msg");
            msg.put("remark", "返回描述");
            msg.put("necessary", "");
            msg.put("type", "String");
            trueExamMock.put("msg", "成功");
            fieldMapList.add(retcode);
            fieldMapList.add(msg);
            if (className.toUpperCase().indexOf("AJAXRESULT") != -1) {
                return JSONObject.toJSONString(fieldMapList, SerializerFeature.WriteMapNullValue);
            }

            Map<String, Object> mockData = Maps.newHashMap();
            Class objClass = Class.forName(className);
            readClassDOFile(objClass);
            Field[] fields = objClass.getDeclaredFields();
            for (Field field : fields) {
                boolean isStatic = Modifier.isStatic(field.getModifiers());
                if (isStatic) {
                    continue;
                }
                Map<String, Object> tmp = Maps.newHashMap();
                tmp.put("name", "data->" + field.getName());
                tmp.put("remark", methodFieldName.get(field.getName()));
                tmp.put("necessary", "true");
                tmp.put("type", field.getType().getSimpleName());
                mockData.put(field.getName(), getValue(field.getType().getSimpleName(), field.getName()));
                fieldMapList.add(tmp);
            }
            trueExamMock.put("data", mockData);
            return JSONObject.toJSONString(fieldMapList, SerializerFeature.WriteMapNullValue);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return "[]";
    }

    static Map<String, String> methodClass = Maps.newHashMap();
    static Map<String, String> methodFieldName = Maps.newHashMap();
    static List<String> importList = Lists.newLinkedList();

    private static void readFile(Class className) {
        String sourcePath = (className.getResource("") + "").replace(className.getClassLoader().getResource("") + "", "");
        String filePath = (className.getResource("/") + "").replace("target/classes/", "/src/main/java/" + sourcePath) + className.getSimpleName() + ".java";
        filePath = filePath.replace("file:", "");
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                if (tempString.startsWith("import")) {
                    importList.add(tempString.replace("import", "").trim());
                }
                getComment(tempString);
                // 显示行号
                if (isNewMethod) {
                    if (tempString.indexOf("Controller") != -1 && !(tempString.indexOf("@RestController") != -1 || tempString.indexOf("@Controller") != -1)) {
                        methodClass.put(file.getName().replace(".java", ""), comment);
                        comment = null;
                        isNewMethod = false;
                    }
                    if (tempString.indexOf("AjaxResult") != -1) {
                        String methodName = getMethodName(tempString);
                        String resultClass = getMethodResult(tempString);
                        methodClass.put(methodName + "_comment", comment);
                        methodClass.put(methodName + "_result_class", resultClass);
                        methodClass.put(methodName + "_result_class_package", getClassPackage(resultClass, importList));
                        comment = null;
                        isNewMethod = false;
                    }
                }
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    private static void readClassDOFile(Class className) {
        methodFieldName = new HashMap<>();
        String sourcePath = (className.getResource("") + "").replace(className.getClassLoader().getResource("") + "", "");
        String filePath = (className.getResource("/") + "").replace("target/classes/", "/src/main/java/" + sourcePath) + className.getSimpleName() + ".java";
        filePath = filePath.replace("file:", "");
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                getClassFieldComment(tempString);
                // 显示行号
                if (isNewFiledComment) {
                    if (tempString.indexOf("private") != -1 || tempString.indexOf("public") != -1 || tempString.indexOf("protected") != -1) {
                        methodFieldName.put(getFiledName(tempString), filedComment);
                        isNewFiledComment = false;
                        filedComment = null;
                    }
                }
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    static boolean isNewFiledCommentReq = false;
    static Map<String, String> methodFieldNameReq = Maps.newHashMap();
    static String filedCommentReq = null;

    //    通读文件，获取字段描述
    private static void readRequestDOFile(Class className) {
        methodFieldNameReq = Maps.newHashMap();
        String sourcePath = (className.getResource("") + "").replace(className.getClassLoader().getResource("") + "", "");
        String filePath = (className.getResource("/") + "").replace("target/classes/", "/src/main/java/" + sourcePath) + className.getSimpleName() + ".java";
        filePath = filePath.replace("file:", "");
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                getClassFieldComment(tempString);
                // 显示行号
                if (isNewFiledCommentReq) {
                    if (tempString.indexOf("private") != -1 || tempString.indexOf("public") != -1 || tempString.indexOf("protected") != -1) {
                        methodFieldNameReq.put(getFiledName(tempString), filedCommentReq);
                        isNewFiledCommentReq = false;
                        filedCommentReq = null;
                    }
                }
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    private static String getClassPackage(String resultClass, List<String> importList) {
        for (String s : importList) {
            s = s.replace(";", "").trim();
            if (s.endsWith(resultClass.trim())) {
                return s;
            }
        }
        return null;
    }


    static Pattern pattern = Pattern.compile("\\<([a-zA-Z]+)\\>");

    private static String getMethodResult(String str) {
        if (StringUtils.isEmpty(str) || str.indexOf("AjaxResult") == -1) {
            return "AjaxResult";
        }
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "AjaxResult";
    }

    static Pattern patternMethod = Pattern.compile("([a-zA-Z0-9]+)\\(");

    private static String getMethodName(String str) {
        Matcher matcher = patternMethod.matcher(str);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }


    static Pattern fieldPattern = Pattern.compile("([a-zA-Z]+);");

    private static String getFiledName(String str) {
        Matcher matcher = fieldPattern.matcher(str);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }


    static String comment = null;
    static boolean isNewMethod = false;

    private static String getComment(String str) {
        if (str.indexOf("/**") != -1) {
            comment = str.replace("/**", "").trim();
            return null;
        }
        if (str.indexOf("*/") != -1) {
            isNewMethod = true;
            return comment;
        }
        if (str.indexOf("*") != -1) {
            if (str.indexOf("@") == -1) {
                if (!StringUtils.isEmpty(str = str.replaceFirst("\\*", "").trim())) {
                    comment = comment + str;
                }
            }
        }
        if (str.indexOf("AjaxResult") != -1 && StringUtils.isNotEmpty(getMethodName(str))) {
            isNewMethod = true;
        }
        return null;
    }


    static String filedComment = null;
    static String fieldCommentTmp = "";
    static boolean isNewFiledComment = false;

//    private static String getClassFieldComment(String str) {
//        if (str.indexOf("/**") != -1) {
//            filedComment = str.replace("/**", "").replace("*/", "").trim();
//            return null;
//        }
//        if (str.indexOf("private") != -1 || str.indexOf("public") != -1 || str.indexOf("protected") != -1) {
//            isNewFiledComment = true;
//            return filedComment;
//        }
//        return null;
//    }

    private static String getClassFieldComment(String str) {
        str = str.trim();
        if (str.indexOf("/**") != -1 && str.endsWith("*/")) {
            filedComment = fieldCommentTmp.replace("/**", "").replace("*/", "").trim();
            fieldCommentTmp = "";
            return null;
        }
        if (str.startsWith("/**")) {
            fieldCommentTmp += str;
            return null;
        }
        if (str.startsWith("*") && !str.startsWith("*/")) {
            System.out.println("str = " + str);
            fieldCommentTmp += str.replace("*", "");
            return null;
        }
        if (str.startsWith("*/")) {
            fieldCommentTmp += str;
            System.out.println(fieldCommentTmp);
            filedComment = fieldCommentTmp.replace("/**", "").replace("*/", "").trim();
            System.out.println("==========" + filedComment);
            fieldCommentTmp = "";
            return null;
        }
        if (str.indexOf("private") != -1 || str.indexOf("public") != -1 || str.indexOf("protected") != -1) {
            isNewFiledComment = true;
            return filedComment;
        }
        return null;
    }


    //获取字段的解释
    private static String getReqClassFieldComment(String str) {
        if (str.indexOf("/**") != -1) {
            filedCommentReq = str.replace("/**", "").replace("*/", "").trim();
            return null;
        }
        if (str.indexOf("private") != -1 || str.indexOf("public") != -1 || str.indexOf("protected") != -1) {
            isNewFiledCommentReq = true;
            return filedCommentReq;
        }
        return null;
    }


    private static Object getValue(String type, String strName) {
        if (type.indexOf("BigDecimal") != -1) {
            return new BigDecimal(new Random().nextInt(9999) / 100f).setScale(2, RoundingMode.HALF_UP);
        }
        if (type.indexOf("Date") != -1) {
            return new Date();
        }
        if (type.indexOf("Integer") != -1) {
            String intNum = getNum(strName);
            if (StringUtils.isNotEmpty(intNum)) {
                return Integer.valueOf(intNum);
            }
            return new Random().nextInt(5);
        }
        if (type.indexOf("Long") != -1) {
            return new Random().nextInt(100) * 1L;
        }
        return genStr(strName);
    }


    private static String genStr(String name) {
        if (StringUtils.isEmpty(name)) {
            return "test_str";
        }
        if (name.toUpperCase().indexOf("NAME") != -1) {
            return "jianyi";
        }
        if (name.toUpperCase().indexOf("DESC") != -1) {
            return "这是一段描述";
        }
        if (name.toUpperCase().indexOf("REMARK") != -1) {
            return "备注内容";
        }
        if (name.toUpperCase().indexOf("CODE") != -1) {
            return "100100";
        }
        if (name.toUpperCase().indexOf("PHONE") != -1) {
            return "18898443355";
        }
        if (name.toUpperCase().indexOf("NUM") != -1 || name.toUpperCase().indexOf("MOBILE") != -1) {
            return "18898443355";
        }
        if (name.toUpperCase().indexOf("ID") != -1) {
            return "10";
        }
        if (name.toUpperCase().indexOf("IMG") != -1) {
            return "http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg";
        }
        if (name.toUpperCase().indexOf("CARD") != -1) {
            return "430981199706087415";
        }
        if (name.toUpperCase().indexOf("PRICE") != -1) {
            return new BigDecimal(new Random().nextInt(1000) / 100f).setScale(2, RoundingMode.HALF_UP) + "";
        }
        String intNum = getNum(name);
        if (StringUtils.isNotEmpty(intNum)) {
            return intNum;
        }
        return "CS_" + getRandomString(new Random().nextInt(30));

    }

    private static String getNum(String name) {
        if (name.toUpperCase().indexOf("LEVEL") != -1) {
            return "1";
        }
        if (name.toUpperCase().indexOf("STATUS") != -1) {
            return "0";
        }
        if (name.toUpperCase().indexOf("DELETE") != -1) {
            return "0";
        }
        if (name.toUpperCase().indexOf("TYPE") != -1) {
            return "1";
        }
        if (name.toUpperCase().indexOf("COUNT") != -1) {
            return "" + new Random().nextInt(99);
        }
        return null;
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String getPostmanId() {
        return ApiDocMain.getRandomString(8).toLowerCase() + "-" +
                ApiDocMain.getRandomString(4).toLowerCase() + "-" +
                ApiDocMain.getRandomString(4).toLowerCase() + "-" +
                ApiDocMain.getRandomString(4).toLowerCase() + "-" +
                ApiDocMain.getRandomString(12).toLowerCase();
    }

    public static void appendMethodFile(String fileName, String content) {
        try {
            System.out.println("文件路径:" + fileName);
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, false);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
